package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.common.entity.Plupload;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.content.entity.HomePage;
import com.dhcc.bpm.modules.content.service.IHomePageService;
import com.dhcc.bpm.modules.content.vo.HomePageVO;
import com.dhcc.bpm.modules.content.vo.HomePageVO2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName HomePageController
 * @Description  门户首页 控制器
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/homepage")
@Api(value = "门户首页", tags = "门户首页接口")
public class HomePageController extends BladeController {

	private IHomePageService homePageService;

	private IPluploadService pluploadService;

	/**
	 * 详情
	 */
	@ApiLog("门户首页模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入homePage")
	public R<HomePage> detail(HomePage homePage) {
		HomePage detail = homePageService.getOne(Condition.getQueryWrapper(homePage));
		Plupload plupload = pluploadService.getById(detail.getBusId());
		HomePageVO homePageVO = new HomePageVO();
		BeanUtil.copy(detail, homePageVO);
		if(plupload != null){
			homePageVO.setFileFullPath(plupload.getFileFullpath());
			homePageVO.setStorageType(plupload.getStorageType());
			homePageVO.setFileRealname(plupload.getFileRealname());
		}
		return R.data(homePageVO);
	}

	/**
	 * 分页 门户首页
	 */
	@ApiLog("门户首页模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入homePage")
	public R<IPage<HomePageVO2>> list(HomePageVO2 homePage, Query query) {
		IPage<HomePageVO2> pages = homePageService.selectHomePagePage2(Condition.getPage(query), homePage);
		return R.data(pages);
	}

	/**
	 * 自定义分页 门户首页
	 */
	@ApiLog("门户首页模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入homePage")
	public R<IPage<HomePageVO>> page(HomePageVO homePage, Query query) {
		IPage<HomePageVO> pages = homePageService.selectHomePagePage(Condition.getPage(query), homePage);
		return R.data(pages);
	}

	/**
	 * 新增 门户首页
	 */
	@ApiLog("门户首页模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入homePage")
	@Log(title = "门户首页", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody HomePage homePage) {
		// 后期改为编号生成器生成
		homePage.setPageCode(UUID.randomUUID().toString());
		homePage.setDocCode(UUID.randomUUID().toString());
		return R.status(homePageService.save(homePage));
	}

	/**
	 * 修改 门户首页
	 */
	@ApiLog("门户首页模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入homePage")
	@Log(title = "门户首页", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody HomePage homePage) {

		return R.status(homePageService.updateById(homePage));
	}

	/**
	 * 新增或修改 门户首页
	 */
	@ApiLog("门户首页模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入homePage")
	@Log(title = "门户首页", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody HomePage homePage) {
		// 后期改为编号生成器生成
		homePage.setPageCode(UUID.randomUUID().toString());
		homePage.setDocCode(UUID.randomUUID().toString());
		return R.status(homePageService.saveOrUpdate(homePage));
	}

	
	/**
	 * 删除 门户首页
	 */
	@ApiLog("门户首页模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "门户首页", businessType = BusinessType.DELETE)
	@Transactional(rollbackFor = Exception.class)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> list = Func.toLongList(ids);
		for(Long id : list){
			HomePage homePage = homePageService.getById(id);
			if(homePage.getBusId() != null ) {
				pluploadService.fileDelete(homePage.getBusId());
			}
		}
		return R.status(homePageService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	* @author: 陈泽
	* @date: 2019/12/11 9:43
	* @description: 修改时删除图片
	* @param: [id, busId]
	* @return: org.springblade.core.tool.api.R
	*/
	@PostMapping("/deleteImage")
	public R deleteImage(Long id, Long busId){
		if(pluploadService.selectById(busId) != null){
			pluploadService.fileDelete(busId);
		}
		HomePage homePage = new HomePage();
		homePage.setId(id);
		homePage.setBusId(-1L);
		return R.status(homePageService.updateById(homePage));
	}
	
}
